<?php

namespace app\admin\controller;

use think\Db;
use think\Request;
use app\admin\model\User as UserModel;

class Con2 extends Con5
{

    public function func1()
    {

//        $list = Db::table('user')->field(['id', 'name'])->order('id', 'asc')->select();
        $list = Db::field('u.name un, r.name rn,u.id uid')
            ->table(['user' => 'u', 'user_role' => 'ur', 'role' => 'r'])
            ->where('u.id = ur.user_id and ur.role_id = r.id')
            ->order(['uid' => 'asc'])
            ->select();
        return view('admin@con2/func1', [
            'list' => $list,
            'name' => $this->name
        ]);
    }

    public function func2()
    {

        return view('admin@con2/func2',[
            'name' => $this->name
        ]);
    }

    public function save(Request $request)
    {
        //ORM
        //实例化model
        $user = new UserModel($request->post());

        //判断两次密码是否一致
        $result = $user->repass(input('post.password'), input('post.repassword'));
        if (!$result) {
            return $this->error('添加失败,两次密码不一致', url('admin/con2/func1'));
        }


        //  验证器 验证数据
        $check = $this->validate($request->post(), 'admin/User');

        if ($check !== true) {
            $this->error($check);
            return;
        }

        // 处理密码
        $user->password = $user->setPass(input('post.password'));
        //         断点调试
//        halt($user->pass);


        // 添加数据,并过滤非数据表字段
        // 返回 写入的记录数
        $result = $user->allowField(true)->save();

        //添加新用户默认给与普通用户角色
        if ($result > 0) {
            //获取普通用户id
            $role_id = Db::name('user')
                ->field('id')
                ->where('name', '=', '普通用户')
                ->find();

            $data = [
                'user_id' => $user->id,
                'role_id' => 3
            ];

            //分配角色
            $result = Db::table('user_role')->insert($data);

        }
//        var_dump($user);

        // 获取自订ID


        // 判断执行情况
        if ($result > 0) {
            return $this->success('添加成功', url('admin/con2/func1'));
        } else {
            return $this->error('添加失败,请重试!');
        }

    }

    //获取角色列表
    public function rolelist($id)
    {


        $sql = "SELECT * FROM `role`";
        $role = Db::query($sql);
        $data = Db::table('user_role')->field('role_id')->where('user_id = ' . $id)->select();
        foreach ($role as $k => $v) {
            $role[$k]['check'] = null;
            foreach ($data as $y) {
                if ($v['id'] == $y['role_id']) {
                    $role[$k]['check'] = 'checked';
                }
            }
        }
        $row = Db::table('user')->field(['name', 'id'])->find($id);


        return view('admin@con2/rolelist', [
            'role' => $role,
            'row' => $row,
            'name' => $this->name
        ]);
    }

    //分配角色
    public function giveRole(Request $request)
    {
        $post = $request->post();
//        halt($post);
        $user_id = $post['user_id'];
        //查询所分配的角色,并删除
        Db::table('user_role')->where('user_id = ' . $user_id)->delete();


        foreach ($post as $k => $v) {
            if ($k !== 'user_id') {
                $data = [
                    'user_id' => $user_id,
                    'role_id' => $v
                ];
                $result = Db::table('user_role')->insert($data);
            }

        }

        $data = Db::table('user_role')->where('user_id = ' . $user_id)->select();
        //如果不选角色默认给与普通用户
        if (!$data) {
            $data = [
                'user_id' => $user_id,
                'role_id' => 3
            ];
            $result = Db::table('user_role')->insert($data);
        }


        if ($result > 0) {
            return $this->success('添加成功', url('admin/con2/func1'));
        } else {
            return $this->error('添加失败,请重试!');
        }
    }

    public function delete()
    {
        echo '111111';
    }

    public function edit()
    {
        echo '11111';
    }

}
